package com.example.myapplication.lawyer;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.LegalexpertiseBean;
import com.example.myapplication.ShuJu.MyconsultationBean;
import com.example.myapplication.apter.ConsultationlistApter;
import com.google.gson.Gson;

public class ConsultationlistActivity extends AppCompatActivity {

    private ImageView img_consultationlist;
    private TextView sort_consultationlist;
    private TextView screen_consultationlist;
    private RecyclerView re_consultationlist;

    private LinearLayout add_consultationlist;
    private Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_consultationlist);
        initview();
        //返回
        but_bloak();
        //点击添加显示添加咨询
        Legalexpertise();

    }

    //刷新我的咨询
    @Override
    protected void onResume() {
        super.onResume();
        //查询我的咨询
        myconsultation();
    }

    private void myconsultation() {
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/legal-advice/list",
                OKHttpTool.GET,
                MyconsultationBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                MyconsultationBean myconsultationBean = (MyconsultationBean) obj;
                                if (myconsultationBean.getCode() == 200) {
                                    //展示列表
                                    consultationlist(myconsultationBean);
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }


    private void Legalexpertise() {
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/legal-expertise/list",
                OKHttpTool.GET,
                LegalexpertiseBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        LegalexpertiseBean legalexpertiseBean = (LegalexpertiseBean) obj;
                        if (legalexpertiseBean.getCode() == 200) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    //添加
                                    addconsultationlist(legalexpertiseBean);
                                }
                            });
                        }
                    }
                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void addconsultationlist(LegalexpertiseBean legalexpertiseBean) {
        add_consultationlist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent intent = new Intent( ConsultationlistActivity.this,AddconsultationlistActivity.class);
                String bean = gson.toJson(legalexpertiseBean);
                intent.putExtra("LawyerListActivity",bean);
                ConsultationlistActivity.this.startActivity(intent);

            }
        });
    }

    //展示列表
    private void consultationlist(MyconsultationBean myconsultationBean) {
        ConsultationlistApter consultationlistApter = new ConsultationlistApter(ConsultationlistActivity.this, myconsultationBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ConsultationlistActivity.this);
        re_consultationlist.setLayoutManager(linearLayoutManager);
        re_consultationlist.setAdapter(consultationlistApter);
    }

    private void but_bloak() {
        img_consultationlist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ConsultationlistActivity.this.finish();
            }
        });
    }

    private void initview() {
        img_consultationlist = findViewById(R.id.img_consultationlist);
        sort_consultationlist = findViewById(R.id.sort_lawyerList);
        screen_consultationlist = findViewById(R.id.screen_lawyerList);
        re_consultationlist = findViewById(R.id.re_consultationlist);
        add_consultationlist = findViewById(R.id.add_consultationlist);
        gson = new Gson();

    }
}